package leecode.算法入门计划.day1;

/**
 * @author cloudJiang
 * @date 2021/10/24 17:19
 */
public class Num35 {
    public static void main(String[] args) {
        System.out.println(searchInsert(new int[]{1,3,5,8}, 9));
    }

    public static int searchInsert(int[] nums, int target) {
        int left = 0, right = nums.length - 1;

        while (left <= right) {
            int mid = left + (right - left) / 2;
            int num = nums[mid];

            if (num == target) {
                return mid;
            } else if (num > target) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }

        return left;
    }
}
